package example.Api.Pay;


import com.alibaba.fastjson.JSONObject;
import example.Data.CommonParameters;
import example.Data.Config;
import example.Data.Key;
import example.Util.HttpClientUtil;
import example.Util.StringUtils;
import example.Util.UtilTest;
import example.pojo.BankApplyPayRequest;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

import static example.Util.StringUtils.*;

/**
 * 网银支付申请接口
 * {
 * 	"app_id": "hyw23030600035360021907331995059",
 * 	"sign_type": "RSA2",
 * 	"timestamp": "2024-07-04 14:29:56",
 * 	"notify_url": "http://demo.hylocal.com/custom/Pay/ReceiveHynetNotify.aspx",
 * 	"biz_content": "{\"bank_code\":\"12\",\"limit_pay\":\"0\",\"return_url\":\"http://demo.hylocal.com\",
 * 	\"out_trade_no\":\"m20240704142956638\",\"out_trade_time\":\"2024-07-04 14:29:56\",
 * 	\"expire_time\":\"2024-07-08 14:29:56\",\"pay_amt\":\"1.00\",
 * 	\"goods_info\":{\"goods_name\":\"gsda\",\"goods_num\":\"1\",\"goods_desc\":\"121\",\"url\":\"http://demo.hylocal.com\"},\"attach\":\"12424\",\"notify_url\":\"http://demo.hylocal.com\",\"is_profit_sharing\":\"true\"}",
 * 	"charset": "utf-8",
 * 	"method": "heepay.pay.bank.apply",
 * 	"version": "1.0",
 * 	"format": "json",
 * 	"sign": "XyESSiykYNgCP9g+VqTj0eSzrjly9XPP/kGrIA3+LGetPF38nlAv/vVKa4vT/VeFOxnyE/a3qLo/p6n+s3H8ZXYDOakAV6v2Ja6obOUhmsXNOq91Xt47BaIlMub7ogk9RdBsz32zqC1eI8evNmzCOlTGON7gLDvHHSRhiJyIPN//+7yDWctkVrixi9kvm+YhsIDUvf6jG8cH70jUnc9By7W5Us9NEo8O0u6mqaneQKH5XRY0VLdc0NThOl7/Ia2Ba4zTooFJY91xcA7hIEZ2JRVq3NoJX1gwi8vrV0CVX3UhHhEeJa713cdOY1tmb8XPf8zvprB8KPwaanY7TTzFtw=="
 * }
 */


public class BankApplyPayApi {

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
        //组装业务请求参数
        BankApplyPayRequest BizRequest = new BankApplyPayRequest();
        BizRequest.setBank_code("12");
        //限制银行卡类型|0=不限制，1=储蓄卡，2=信用卡 默认: 0
        BizRequest.setLimit_pay(0);
        //之后可能优化，直留公共请求参数中的notify_url
        BizRequest.setReturn_url("http://demo.hylocal.com");
        BizRequest.setOut_trade_no(get14Timestamp());
        BizRequest.setOut_trade_time(getNowTime());

        //订单有效期，格式：yyyy-MM-dd HH:mm:ss
        //目前代码写加1分钟，实际上应该根据订单实际情况来确定
        BizRequest.setExpire_time(getCurrentTimePlusMinutes(1));


        BizRequest.setPay_amt("1.00");
        BizRequest.setAttach("Test");
        BizRequest.setNotify_url("http://demo.hylocal.com");
        BizRequest.setIs_profit_sharing("true");
        BizRequest.setGoods_info("{\"goods_name\":\"goods\",\"goods_num\":\"1\",\"goods_desc\":\"121\",\"url\":\"http://demo.hylocal.com\"}");

        String bizContent = JSONObject.toJSONString(BizRequest);

        System.out.println("业务请求参数：-------------------------------->："+bizContent);
        System.out.println();

        //组装签名串
        Map<String, String> signMapStr  = new HashMap<>();
        signMapStr.put("app_id", CommonParameters.app_id);
        signMapStr.put("method", Config.ConfigPayMethod.WyMethod);
        signMapStr.put("timestamp", getNowTime());
        signMapStr.put("notify_url", CommonParameters.notify_url);
        signMapStr.put("biz_content", bizContent);
        signMapStr.put("charset", CommonParameters.charset);
        signMapStr.put("version", CommonParameters.version);
        signMapStr.put("sign_type", CommonParameters.sign_type);
        signMapStr.put("format", CommonParameters.format);

        String signStr = StringUtils.mapToStringSortedByKey(signMapStr);


        System.out.println("网银支付签名串：-------------------------------->："+signStr);
        System.out.println();

        //对签名串进行RSA2签名
        String sign = UtilTest.rsa2Sign(Key.PRIVATE_KEY,signStr,CommonParameters.charset);
        System.out.println("网银支付签名结果：-------------------------------->："+sign);
        System.out.println();

        //组装请求参数（公共参数）
        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("app_id", CommonParameters.app_id);
        requestMap.put("method", Config.ConfigPayMethod.WyMethod);
        requestMap.put("timestamp", getNowTime());
        requestMap.put("notify_url", CommonParameters.notify_url);
        requestMap.put("biz_content", bizContent);
        requestMap.put("charset", CommonParameters.charset);
        requestMap.put("version", CommonParameters.version);
        requestMap.put("sign_type", CommonParameters.sign_type);
        requestMap.put("format", CommonParameters.format);
        requestMap.put("sign", sign);

        System.out.println("网银支付请求参数：-------------------------------->："+JSONObject.toJSONString(requestMap));
        System.out.println();

        //发送请求
        String response = HttpClientUtil.sendPostJson(Config.ConfigUrl.INTERNAL_API_URL, requestMap);
        System.out.println("网银支付响应结果：-------------------------------->："+response);
        System.out.println();


    }



}
